package cn.codelab.springcloudalibaba.study.step07.controller;

import com.alibaba.csp.sentinel.annotation.SentinelResource;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * @author Seven
 * @description
 * @博客：https://codelab7.cn
 * @Gitee https://gitee.com/it-codelab
 * @Copyright 公众号：Seven的代码实验室
 */
@RestController
@RequestMapping("/user")
public class UserController {

    @SentinelResource(value = "getUserById")
    @RequestMapping("/{id}")
    public String getUserById(@PathVariable Long id) {
        // 模拟从数据库中查找用户信息
        Map<Long, String> map = new HashMap<>(0);
        map.put(1L, "user1");
        map.put(2L, "user2");
        map.put(3L, "user3");
        return map.get(id);
    }

    @RequestMapping("/getUser1")
    public String getUser1() {
        // 模拟从数据库中查找用户信息
       return this.getUser(1L);
    }

    @RequestMapping("/getUser2")
    public String getUser2() {
        // 模拟从数据库中查找用户信息
        return this.getUser(2L);
    }

    @SentinelResource(value = "getUser")
    public String getUser(Long id) {
        // 模拟从数据库中查找用户信息
        Map<Long, String> map = new HashMap<>(0);
        map.put(1L, "user1");
        map.put(2L, "user2");
        map.put(3L, "user3");
        return map.get(id);
    }
}
